/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.views;

import com.insurance.cis.connection.CisConnection;
import com.insurance.cis.connection.JdbcConfig;
import com.insurance.cis.utils.MessageDialogUtils;
import com.insurance.cis.utils.Utils;
import java.sql.SQLException;


/**
 *
 * @author TruongLQ
 */
public class DbConfigurationJPanelView extends javax.swing.JPanel {

    /**
     * Creates new form DbConfigurationJPanelView
     */
    public DbConfigurationJPanelView(javax.swing.JFrame jFrame) {
        initComponents();
        loadDbConfig(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnDefault = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtDatabaseName = new javax.swing.JTextField();
        btnSave = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        txtServerName = new javax.swing.JTextField();
        txtPort = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtDbConfigUsername = new javax.swing.JTextField();
        txtDbConfigPassword = new javax.swing.JPasswordField();
        btnTestConnection = new javax.swing.JButton();

        btnDefault.setText("Default");
        btnDefault.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDefaultActionPerformed(evt);
            }
        });

        jLabel3.setText("User name");

        jLabel4.setText("Password");

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        jLabel5.setText("Database name");

        jLabel6.setText("Server name");

        jLabel7.setText("Port");

        btnTestConnection.setText("Test connection");
        btnTestConnection.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTestConnectionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(265, 265, 265)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txtServerName))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(btnTestConnection)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(btnSave))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(47, 47, 47)
                                        .addComponent(txtDbConfigPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel3))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(txtDbConfigUsername, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtDatabaseName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtPort, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(btnDefault))))
                .addContainerGap(284, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(153, 153, 153)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtServerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtDatabaseName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(txtPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtDbConfigUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtDbConfigPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSave)
                    .addComponent(btnDefault)
                    .addComponent(btnTestConnection))
                .addContainerGap(290, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnDefaultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDefaultActionPerformed
        loadDbConfig(true);
    }//GEN-LAST:event_btnDefaultActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        if (!isDbConfigValidInputs()) {
            return;
        }
        JdbcConfig config = new JdbcConfig(txtServerName.getText(),
            txtDatabaseName.getText(), txtPort.getText(),
            txtDbConfigUsername.getText(),
            String.valueOf(txtDbConfigPassword.getPassword()));
        Utils.saveJdbcConfig(config);
        MessageDialogUtils.dialogMessage(this, "Successful saving database server configuration");
    }//GEN-LAST:event_btnSaveActionPerformed

    private boolean isDbConfigValidInputs() {
        if (txtServerName.getText().isEmpty()) {
            MessageDialogUtils.dialogError(this, "Server name must not be blank!");
            txtServerName.requestFocus();
            return false;
        } 
        if (txtDatabaseName.getText().isEmpty()) {
            MessageDialogUtils.dialogError(this, "Database name must not be blank!");
            txtDatabaseName.requestFocus();
            return false;
        } 
        if (txtPort.getText().isEmpty()) {
            MessageDialogUtils.dialogError(this, "Port must not be blank!");
            txtPort.requestFocus();
            return false;
        }
        if (txtDbConfigUsername.getText().isEmpty()) {
            MessageDialogUtils.dialogError(this, "User name must not be blank!");
            txtDbConfigUsername.requestFocus();
            return false;
        }
        return true;
    }
    private void loadDbConfig(boolean isDefault) {
        JdbcConfig config = new JdbcConfig();
        if (!isDefault) {
            config = Utils.loadJdbcConfig();
        }
        txtServerName.setText(config.getProtocolHost());
        txtDatabaseName.setText(config.getDatabaseName());
        txtPort.setText(config.getProtocolPort());
        txtDbConfigUsername.setText(config.getUsername());
        txtDbConfigPassword.setText(config.getPassword());
    }
    
    private void testConnection() {
        JdbcConfig config = new JdbcConfig();
        config.setProtocolHost(txtServerName.getText());
        config.setProtocolPort(txtPort.getText());
        config.setDatabaseName(txtDatabaseName.getText());
        config.setUsername(txtDbConfigUsername.getText());
        config.setPassword(String.valueOf(txtDbConfigPassword.getPassword()));
        try {
            CisConnection.getInstance().getSqlConnection(config);
            MessageDialogUtils.dialogMessage(this, "Successful database connection");
        } catch (SQLException ex) {
            String msg ="Could not establish connection to database\n";
            MessageDialogUtils.dialogError(null, msg);
        }
    }
    
    private void btnTestConnectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTestConnectionActionPerformed
        testConnection();
    }//GEN-LAST:event_btnTestConnectionActionPerformed

    public void clearAllTextFields() {
        txtDatabaseName.setText("");
        txtDbConfigPassword.setText("");
        txtDbConfigUsername.setText("");
        txtPort.setText("");
        txtServerName.setText("");
        btnDefaultActionPerformed(null);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDefault;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnTestConnection;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField txtDatabaseName;
    private javax.swing.JPasswordField txtDbConfigPassword;
    private javax.swing.JTextField txtDbConfigUsername;
    private javax.swing.JTextField txtPort;
    private javax.swing.JTextField txtServerName;
    // End of variables declaration//GEN-END:variables
}
